# -*- coding: utf-8 -*-
import scrapy,re
from scrapy_redis.spiders import RedisSpider
from slavegoodread.items import BookItem

class GoodreadSpider(RedisSpider):
    name = 'goodread'
    #allowed_domains = ['goodreads.com']
    #start_urls = ['http://goodreads.com/']
    redis_key = 'booksdetail:start_urls'
    print("GoodreadSpider Start")
    print("GoodreadSpider Start")
    print("GoodreadSpider Start")
    #https://www.goodreads.com/book/show/18137847-against-all-odds
    #lpush booksdetail:start_urls https://www.goodreads.com/book/show/165030.Anybody_Out_There_
    #lpush booksdetail:start_urls https://www.goodreads.com/book/show/27406704-britt-marie-was-here
    
    def __init__(self, *args, **kwargs):
        
        domain = kwargs.pop('domain','')
        self.allowed_domains = filter(None, domain.split(','))
        super(GoodreadSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        print("=======================",response.status)
        print("=======================",response.status)
        print("=======================",response.status)
        book = BookItem()
        #vo = response.css("div#asyncBuyButtonContainer")
        book['id'] = response.css("#asyncBuyButtonContainer::attr(data-book-id)").extract_first()
        if response.css("h1#bookTitle::text").extract_first():
            book['title'] = response.css("h1#bookTitle::text").extract_first().strip().replace("\n","")
        book['author'] = response.css("a.authorName span::text").extract_first()
        vo = response.css("div#bookDataBox").extract_first()
        #print(vo)
        if vo:
            if re.search('itemprop="inLanguage">(.*?)</div>',vo,re.S):
                book['language'] = re.search('itemprop="inLanguage">(.*?)</div>',vo,re.S).group(1)
            if re.search('class="infoBoxRowTitle">Original Title</div>.*?<div class="infoBoxRowItem">(.*?)</div>',vo,re.S):
                book['original'] = re.search('class="infoBoxRowTitle">Original Title</div>.*?<div class="infoBoxRowItem">(.*?)</div>',vo,re.S).group(1)
            if re.search('class="infoBoxRowTitle">ISBN</div>.*?<div class="infoBoxRowItem">(.*?)<',vo,re.S):
                book['isbn'] = re.search('class="infoBoxRowTitle">ISBN</div>.*?<div class="infoBoxRowItem">(.*?)<',vo,re.S).group(1).strip().replace("\n","")
            if re.search('<div class="infoBoxRowTitle">Series</div>.*?<div class="infoBoxRowItem">.*?<a.*?>(.*?)</a>',vo,re.S):
                book['series'] = re.search('<div class="infoBoxRowTitle">Series</div>.*?<div class="infoBoxRowItem">.*?<a.*?>(.*?)</a>',vo,re.S).group(1)
        vo = response.css("div#details").extract_first()
        #print(vo)
        if vo:
            if re.search('<span itemprop="numberOfPages">(.*?)</span>',vo,re.S):
                book['pages'] = re.search('<span itemprop="numberOfPages">(.*?)</span>',vo,re.S).group(1)
            if re.search('<div class="row">.*?Published(.*?)by ',vo,re.S):
                book['imprint'] = re.search('<div class="row">.*?Published(.*?)by ',vo,re.S).group(1).strip().replace("\n","")
            if re.search('<div class="row">.*?Published.*?by (.*?)<',vo,re.S):
                book['press'] = re.search('<div class="row">.*?Published.*?by (.*?)<',vo,re.S).group(1).strip().replace("\n","")
        if response.css("span.average::text").extract_first():
            book['score'] = response.css("span.average::text").extract_first().strip().replace("\n","")
        if response.css('span.votes::text').extract_first():
            book['number'] = response.css('span.votes::text').extract_first().strip().replace("\n","")
        print(book)
        return(book)
        
